package com.tsd.jxc.fin;

import cn.hutool.db.Entity;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.tsd.common.dao.CommonParameterValueMapper;
import com.tsd.common.entity.CommonParameterValue;
import com.tsd.common.entity.CommonParameterValueExt;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.datasource.DataSourceInitService;
import com.tsd.core.utils.HlpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: StartAppRunner
 * @Description: 服务初始化之后，自动执行方法
 * @Author: Hillpool
 * @Date: 2022/10/14 09:55
 * @Version: 1.0
 */
@Component
@Order(100)
public class FinStartAppRunner implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(FinStartAppRunner.class);

    @Resource
    private CommonParameterValueMapper commonParameterValueMapper;

    @Override
    public void run(String... args) throws Exception {
        LOGGER.info("初始化自定义的账单类型 start...");
        this.initAccountType();
        LOGGER.info("初始化自定义的账单类型 end...");
    }

    private void initAccountType() {
        Map<Object, Entity> map = DataSourceInitService.getAccountSetMap();
        if (map == null || map.isEmpty()) {
            return;
        }
        Set<Object> dataSourceKey = map.keySet();
        for (Object key : dataSourceKey) {
            Entity accountSetExt = map.get(key);
            if (accountSetExt == null) {
                continue;
            }
            String modeStr = accountSetExt.getStr("mode");
            List<String> modeList = new ArrayList<>();
            if (!HlpUtils.isEmpty(modeStr) && JSONUtil.isJsonArray(modeStr)) {
                modeList = JSON.parseArray(modeStr, String.class);
            }
            if (modeList.contains(FinanceConstants.MODE_JXC)) {
                this.initAccountType(accountSetExt);
            }
        }
    }

    private void initAccountType(Entity accountSetExt) {
        String sid = accountSetExt.getStr("sid");
        DataSourceContextHolder.setDataSourceType(sid);
        CommonParameterValueExt params = new CommonParameterValueExt();
        params.setDr(0);
        params.setPre_set(0);
        params.setKey1(FinanceConstants.KEY_JXC_ACCOUNT_TYPE);
        List<CommonParameterValue> list = commonParameterValueMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(list)) {
            return;
        }
        for (CommonParameterValue value : list) {
            String key = String.format("%s@%s", sid, value.getKey3());
            FinanceConstants.setAccountType(key, value.getDisplay_text());
            FinanceConstants.setAccountTypeOp(key, value.getKey2());
        }
    }
}
